package org.jeecg.modules.htxincailiao.policy.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RequestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.news.entity.HTNews;
import org.jeecg.modules.htxincailiao.policy.entity.HtPolicy;
import org.jeecg.modules.htxincailiao.policy.service.IHtPolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 政策
 * @Author: jeecg-boot
 * @Date: 2024-07-09
 * @Version: V1.0
 */
@Api(tags = "政策")
@RestController
@RequestMapping("/htPolicy")
@Slf4j
public class HtPolicyController extends JeecgController<HtPolicy, IHtPolicyService> {
    @Autowired
    private IHtPolicyService htPolicyService;

    /**
     * 分页列表查询
     *
     * @param htPolicy
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "政策-分页列表查询")
    @ApiOperation(value = "政策-分页列表查询", notes = "政策-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<HtPolicy>> queryPageList(HtPolicy htPolicy,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req) {
        QueryWrapper<HtPolicy> queryWrapper = QueryGenerator.initQueryWrapper(htPolicy, req.getParameterMap());
        LambdaQueryWrapper<HtPolicy> lambdaQueryWrapper = queryWrapper.lambda();
        lambdaQueryWrapper.orderByAsc(HtPolicy::getSort);
        lambdaQueryWrapper.orderByDesc(HtPolicy::getCreateTime);
        Page<HtPolicy> page = new Page<>(pageNo, pageSize);
        IPage<HtPolicy> pageList = htPolicyService.page(page, lambdaQueryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "htPolicy-分页列表查询")
    @ApiOperation(value = "htPolicy-分页列表查询", notes = "htPolicy-分页列表查询")
    @RequestMapping(value = "/pc-home-list", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryPCHomePageList(HtPolicy htPolicy,
                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         HttpServletRequest req) {
        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                htPolicy = JSONObject.parseObject(line, HtPolicy.class);
                pageNo = oConvertUtils.isNotEmpty(htPolicy.getPageNo()) ? htPolicy.getPageNo() : pageNo;
                pageSize = oConvertUtils.isNotEmpty(htPolicy.getPageSize()) ? htPolicy.getPageSize() : pageSize;
            }
        }

        if (oConvertUtils.isNotEmpty(htPolicy) &&
                oConvertUtils.isNotEmpty(htPolicy.getCategoryId()) &&
                htPolicy.getCategoryId().equals("0")) {
            //全部分类
            htPolicy.setCategoryId(null);
        }


        QueryWrapper<HtPolicy> queryWrapper = QueryGenerator.initQueryWrapper(htPolicy, req.getParameterMap());
        queryWrapper.orderByDesc("is_home_recommended").orderByAsc("sort").orderByDesc("create_time");

        Page<HtPolicy> page = new Page<>(pageNo, pageSize);
        IPage<HtPolicy> pageList = htPolicyService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param htPolicy
     * @return
     */
    @AutoLog(value = "政策-添加")
    @ApiOperation(value = "政策-添加", notes = "政策-添加")
    //@RequiresPermissions("htxincailiao:ht_policy:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody HtPolicy htPolicy) {
        htPolicyService.save(htPolicy);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param htPolicy
     * @return
     */
    @AutoLog(value = "政策-编辑")
    @ApiOperation(value = "政策-编辑", notes = "政策-编辑")
    //@RequiresPermissions("htxincailiao:ht_policy:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody HtPolicy htPolicy) {
        htPolicyService.updateById(htPolicy);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "政策-通过id删除")
    @ApiOperation(value = "政策-通过id删除", notes = "政策-通过id删除")
    //@RequiresPermissions("htxincailiao:ht_policy:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        htPolicyService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "政策-批量删除")
    @ApiOperation(value = "政策-批量删除", notes = "政策-批量删除")
    //@RequiresPermissions("htxincailiao:ht_policy:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.htPolicyService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "政策-通过id查询")
    @ApiOperation(value = "政策-通过id查询", notes = "政策-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<HtPolicy> queryById(@RequestParam(name = "id", required = true) String id) {
        HtPolicy htPolicy = htPolicyService.getById(id);
        if (htPolicy == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(htPolicy);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param htPolicy
     */
    //@RequiresPermissions("htxincailiao:ht_policy:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HtPolicy htPolicy) {
        return super.exportXls(request, htPolicy, HtPolicy.class, "政策");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("htxincailiao:ht_policy:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HtPolicy.class);
    }

}
